package com.dz51.core.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;

/**
 * 工具类 - 公用
 */

public class CommonUtil {

	/**
	 * 随机获取UUID字符串(无中划线)
	 * 
	 * @return UUID字符串
	 */
	public static String getUUID() {
		String uuid = UUID.randomUUID().toString();
		return uuid.substring(0, 8) + uuid.substring(9, 13) + uuid.substring(14, 18) + uuid.substring(19, 23) + uuid.substring(24);
	}

	public static String getLuckNumber(int winningRate) {
		String luckNumber = "";
		int number = 0;
		if (winningRate == 1) {
			// 100%
			number = (int) (Math.random() * 1);
		} else if (winningRate == 2) {
			// 10%
			number = (int) (Math.random() * 10);
		} else if (winningRate == 3) {
			// 1%
			number = (int) (Math.random() * 100);
		} else if (winningRate == 4) {
			// 0.1%
			number = (int) (Math.random() * 1000);
		} else if (winningRate == 5) {
			// 0.01%
			number = (int) (Math.random() * 10000);
		} else if (winningRate == 6) {
			// 0.001%
			number = (int) (Math.random() * 100000);
		} else if (winningRate == 7) {
			// 0.0001%
			number = (int) (Math.random() * 1000000);
		} else if (winningRate == 8) {
			// 0.00001%
			number = (int) (Math.random() * 10000000);
		} else {
			// 0.000001%
			number = (int) (Math.random() * 100000000);
		}
		luckNumber = String.valueOf(number);
		return luckNumber;
	}

	/**
	 * 随机获取字符串
	 * 
	 * @param length
	 *            随机字符串长度
	 * 
	 * @return 随机字符串
	 */
	public static String getRandomString(int length) {
		if (length <= 0) {
			return "";
		}
		char[] randomChar = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'a', 's', 'd', 'f',
				'g', 'h', 'j', 'k', 'l', 'z', 'x', 'c', 'v', 'b', 'n', 'm' };
		Random random = new Random();
		StringBuffer stringBuffer = new StringBuffer();
		for (int i = 0; i < length; i++) {
			stringBuffer.append(randomChar[Math.abs(random.nextInt() - 1) % randomChar.length]);
		}
		return stringBuffer.toString();
	}

	/**
	 * 根据指定长度 分隔字符串
	 * 
	 * @param str
	 *            需要处理的字符串
	 * @param length
	 *            分隔长度
	 * 
	 * @return 字符串集合
	 */
	public static List<String> splitString(String str, int length) {
		List<String> list = new ArrayList<String>();
		for (int i = 0; i < str.length(); i += length) {
			int endIndex = i + length;
			if (endIndex <= str.length()) {
				list.add(str.substring(i, i + length));
			} else {
				list.add(str.substring(i, str.length() - 1));
			}
		}
		return list;
	}

	/**
	 * 将字符串List转化为字符串，以分隔符间隔.
	 * 
	 * @param list
	 *            需要处理的List.
	 * 
	 * @param separator
	 *            分隔符.
	 * 
	 * @return 转化后的字符串
	 */
	public static String toString(List<String> list, String separator) {
		StringBuffer stringBuffer = new StringBuffer();
		for (String str : list) {
			stringBuffer.append(separator).append(str);
		}
		stringBuffer.deleteCharAt(0);
		return stringBuffer.toString();
	}
}